var index = parent.layer.getFrameIndex(window.name);
var currentBlockId = parent.$("#blockid").val();
var blockName = parent.$("#"+currentBlockId).children('.uitext').text();
var inputC = parent.$("#blockid").val()+"input";
var paramsC = parent.$("#blockid").val()+"params";
var inputC_value = parent.$("#"+inputC).val();
var paramsC_value = parent.$("#"+paramsC).val();
var currentAlgorithmId = parent.$("#algorithmid").val();
var frontAlgorithmIds = parent.$("#frontAlgorithmIds").val();
var json_frontAlgorithmIds = eval("("+frontAlgorithmIds+")");
var isFirst = false;
var dataId = "";
var dataIdNumber = 1;
window.onload = function () {
    $.ajax({
        url:constUrl+"ws/model",
        type:'post',
        dataType:'json',
        data:{
            "operateType":"configModel",
            "data":JSON.stringify({
                "currentAlgorithmId":currentAlgorithmId,
                "currentBlockId":currentBlockId,
                "frontAlgorithmIds":json_frontAlgorithmIds
            })
        },
        success: function(responseText) {
            var jsonData = responseText;
            creatHtml(jsonData);
            $("#blockName").val(blockName);
            if(inputC_value!=""){
                var inputC_json = eval("("+inputC_value+")");
                if(!isFirst){
                    for(var i=0; i<inputC_json.length; i++){
                        $(".input_name_text").each(function (index, elem) {
                            var $elem = $(elem);
                            if($elem.html()==inputC_json[i].inputName) {
                                if (inputC_json[i].frontOutput.blockId==dataId) {
                                    $elem.parent().parent().parent().children('select').find('option[value="+' + dataId + '+"]').attr("selected", true);
                                    $elem.parent().parent().parent().children('select').after('<input type="text" class="form-group form-control dataName" placeholder="请输入外部数据源名字">');
                                    $(".dataName").val(inputC_json[i].frontOutput.frontOutputName.split("+")[0]);
                                } else {
                                    $elem.parent().parent().parent().children('select').find('option[value="' + inputC_json[i].frontOutput.frontOutputName + '+' + inputC_json[i].frontOutput.blockId + '+' + inputC_json[i].frontOutput.dataId + '"]').attr("selected", true);
                                }
                            }
                        });
                    }
                }
            }
            if(paramsC_value!=""){
                var paramsC_json = eval("("+paramsC_value+")");
                for(var i=0; i<paramsC_json.length; i++){
                    $(".param_name_text").each(function (index, elem) {
                        var $elem = $(elem);
                        if($elem.html()==paramsC_json[i].paramName){
                            $elem.parent().parent().parent().children('input').val(paramsC_json[i].paramValue);
                        }
                    });
                }
            }
        },
        complete: function() {},
        error: function() {
            layer.alert("获取数据失败！")
        }
    })
    $('#saveSetting').click(function () {
        parent.$("#"+currentBlockId).children('.uitext').text($("#blockName").val());
        var blocks = [];
        $(".param").each(function (index, elem) {
            var $elem = $(elem);
            blocks.push({
                paramName: $elem.children('.param_name').children('label').children(".param_name_text").text(),
                paramValue: $elem.children('input').val(),
                type: $elem.children('.param_name').children('.type').val(),
                desc: $elem.children('.param_name').children('.desc').val()
            });
        });
        var json_blocks = JSON.stringify(blocks);
        window.parent.document.getElementById(paramsC).value=json_blocks;
        if(isFirst){
        }else{
            var inputConfig = [];
            $(".input").each(function (index, elem) {
                var $elem = $(elem);
                var zhi = $elem.children('select').find("option:selected").val();
                var input_type = $elem.children('select').attr("input-type");
                if(zhi.split("+")[1]==dataId){
                    var zhi_json_str = '{"frontOutputName":"' + $elem.children(".dataName").val() + '+' + zhi.split("+")[1] + '+' + dataIdNumber + '","blockId":"' + zhi.split("+")[1] + '","dataId":"' + zhi.split("+")[2] + '","type":"'+ input_type +'"}';
                    dataIdNumber++;
                }
                else{
                    var zhi_json_str = '{"frontOutputName":"' + zhi.split("+")[0] + '","blockId":"' + zhi.split("+")[1] + '","dataId":"' + zhi.split("+")[2] + '","type":"'+ input_type +'"}';
                }
                var zhi_json = eval("(" + zhi_json_str + ")");
                inputConfig.push({
                    inputName: $elem.children('.input_name').children('label').children(".input_name_text").text(),
                    frontOutput: zhi_json
                });
                var json_inputConfig = JSON.stringify(inputConfig);
                window.parent.document.getElementById(inputC).value = json_inputConfig;
            });
        }
        $("#"+paramsC, window.parent.document).parent().children(".uidiv").children(".progressbar").children("div").attr("style","width: 100%");
        //修改名称
        var modifyBlockName = $("#blockName").val();
        parent.$("#currentBlockId").children('.uitext').text(modifyBlockName);

        parent.layer.close(index);
    });
    $('#closeIframe').click(function(){
        parent.layer.close(index);
    });
}
creatHtml = function(jsonData){
    dataId = jsonData.blockId;
    var param_form = document.getElementById("param_form");
    var paramsConfig = jsonData.currentAlgorithmDetail.algorithmParams;
    param_form.innerHTML +=
            '<div class="form-title">' +
                '<h4>参数配置</h4>' +
            '</div>';
    for(var i=0;i<paramsConfig.length;i++){
        param_form.innerHTML +=
            '<hr>' +
            '<div class="param">' +
                '<div class="form-group param_name"> ' +
                    '<label class="col-sm-2">参数符号:<i class="param_name_text">'+paramsConfig[i].paramSymbol+'</i></label> ' +
                    '<input type="hidden" class="type" value="'+paramsConfig[i].paramType+'">' +
                    '<input type="hidden" class="desc" value="'+paramsConfig[i].paramIntroduction+'">' +
                '</div> ' +
                '<input type="text" class="form-group form-control param-one" id="Param'+i+'" value="'+paramsConfig[i].paramDefaultValue+'"> ' +
                '<div class="form-group"> ' +
                    '<label class="col-sm-2 control-label">简介</label> ' +
                    '<div class="col-sm-10"> ' +
                        '<p class="form-control-static">'+paramsConfig[i].paramIntroduction+'</p> ' +
                    '</div> ' +
                '</div>' +
            '</div> ';
    }
    param_form.innerHTML += '<div style="clear:both"></div>';
    var input_form = document.getElementById("input_form");
    input_form.innerHTML +=
        '<div class=\"form-title\">' +
            '<h4>输入配置</h4>' +
        '</div>';
    if(jsonData.frontAlgorithmDetails!=null){
        for(var i=0; i<jsonData.currentAlgorithmDetail.algorithmInputs.length; i++){
            input_form.innerHTML +=
                '<hr>' +
                '<div class="input">' +
                    '<div class="form-group input_name"> ' +
                        '<label class="col-sm-2">输入名称:<i class="input_name_text">'+jsonData.currentAlgorithmDetail.algorithmInputs[i].inputName+'</i></label> ' +
                    '</div> ' +
                    '<select class="form-group form-control sl" input-type="'+jsonData.currentAlgorithmDetail.algorithmInputs[i].type+'">' +
                        '<option value="none" disabled="disabled" selected="selected" >请选择输入</option>'+
                    '</select>' +
                    '<div class="form-group"> ' +
                        '<label class="col-sm-2 control-label">简介</label> ' +
                        '<div class="col-sm-10"> ' +
                            '<p class="form-control-static">'+jsonData.currentAlgorithmDetail.algorithmInputs[i].inputIntroduction+'</p> ' +
                        '</div> ' +
                    '</div> ' +
                '</div>';
        }
        for(var n=0; n<jsonData.frontAlgorithmDetails.length; n++){
            for(var m=0; m<jsonData.frontAlgorithmDetails[n].algorithmOutputs.length; m++){
                $(".sl").each(function(index, element) {
                    var $this = $(this);
                    var input_type = $this.attr("input-type");
                    if(input_type == jsonData.frontAlgorithmDetails[n].algorithmOutputs[m].type){
                        var text = '<option value="'+jsonData.frontAlgorithmDetails[n].algorithmOutputs[m].outputName+'+'+jsonData.frontAlgorithmDetails[n].blockId+'+'+jsonData.frontAlgorithmDetails[n].id+'">前置组件名：'+jsonData.frontAlgorithmDetails[n].blockName+'，输出名：'+jsonData.frontAlgorithmDetails[n].algorithmOutputs[m].outputName+'</option>';
                    } else {
                        var text = '<option disabled="disabled" value="'+jsonData.frontAlgorithmDetails[n].algorithmOutputs[m].outputName+'+'+jsonData.frontAlgorithmDetails[n].blockId+'+'+jsonData.frontAlgorithmDetails[n].id+'" style="color: #c1c1c1;">前置组件名：'+jsonData.frontAlgorithmDetails[n].blockName+'，输出名：'+jsonData.frontAlgorithmDetails[n].algorithmOutputs[m].outputName+'</option>';

                    }
                    $this.append(text);
                });
            }
        }
        if(dataId!=""&&dataId!="undefined"){
            $(".sl").each(function(index, element) {
                var $this = $(this);
                var text = '<option value="+'+dataId+'+">外部数据源</option>';
                $this.append(text);
            });
        }
    }else{
        input_form.innerHTML += "<hr><div style='padding-left: 15px;'>无</div>";
        isFirst = true;
    }
    input_form.innerHTML += '<div style="clear:both"></div>';
    $(".sl").change(function(){
        var result =$(this).children('option:selected').val();
        if(result=="+"+dataId+"+"){
            var input = $(this);
            input.after('<input type="text" class="form-group form-control dataName" placeholder="请输入外部数据源名字">');
        }else{
            var input = $(this).parent();
            input.children(".dataName").remove();
        }
    });
}
